package lendingbooks.activities;

import java.util.ArrayList;
import java.util.Collection;

import lendingbooks.copy.Copy;
import lendingbooks.copy.CopyListAdapter;
import lendingbooks.copy.CopyListViewRow;
import lendingbooks.copy.CopyManager;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import com.markupartist.android.widget.PullToRefreshListView;
import com.markupartist.android.widget.PullToRefreshListView.OnRefreshListener;

public class MyLibraryActivity extends Activity {

	private Collection<Copy> myLibraryBooks = null;
	private ListView listViewMyLibrary = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_library);
		listViewMyLibrary = (ListView) findViewById(R.id.listViewMyLibrary);
		refreshMyLibraryBookList();
		((PullToRefreshListView) listViewMyLibrary).setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				refreshMyLibraryBookList();
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.d("MyLibraryActivity", "((LendingBooksApplication) getApplication()).isMyLibraryChanged() [" + ((LendingBooksApplication) getApplication()).isMyLibraryChanged()
				+ "]");
		if (((LendingBooksApplication) getApplication()).isMyLibraryChanged()) {
			((LendingBooksApplication) getApplication()).setMyLibraryChanged(false);
			refreshMyLibraryBookList();
		}
	}

	public void refreshMyLibraryBookList() {
		final ProgressDialog dialog = ProgressDialog.show(MyLibraryActivity.this, "", "Loading. Please wait...", true);
		final Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				ArrayList<CopyListViewRow> myLibraryBooksListViewRow = new ArrayList<CopyListViewRow>();
				for (Copy copy : myLibraryBooks) {
					myLibraryBooksListViewRow.add(new CopyListViewRow(copy));
				}

				CopyListAdapter copyListAdapter = new CopyListAdapter(listViewMyLibrary.getContext(),
						R.layout.list_copy_row, myLibraryBooksListViewRow);
				listViewMyLibrary.setAdapter(copyListAdapter);
				copyListAdapter.notifyDataSetChanged();
				listViewMyLibrary.setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
						CopyListViewRow copyListViewRow = (CopyListViewRow) parent.getItemAtPosition(position);
						if (copyListViewRow.getIntent() == null) {
							copyListViewRow.setIntent(new Intent(getApplicationContext(), CopyActivity.class));
						}
						copyListViewRow.getIntent().putExtra("copy", copyListViewRow.getCopy());
						startActivity(copyListViewRow.getIntent());
					}
				});
				((LendingBooksApplication) getApplication()).setMyLibraryChanged(false);
				((PullToRefreshListView) listViewMyLibrary).onRefreshComplete();
				dialog.dismiss();
			}
		};

		Thread loadLibrary = new Thread() {
			@Override
			public void run() {
				CopyManager cm = new CopyManager();
				myLibraryBooks = cm.getMyLibrary();
				Message message = new Message();
				handler.sendMessage(message);
			}
		};
		loadLibrary.start();
	}
}
